<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/taglibs.jsp"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>访问token</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="${ctxLayui}/layuiadmin/layui/css/layui.css" media="all">
	<link rel="stylesheet" href="${ctxLayui}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
	<div class="layui-breadcrumb" lay-filter="breadcrumb">
		<!--<a lay-href="">xx管理</a>-->
		<a><cite>访问token</cite></a>
	</div>
</div>

<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body">


					<div class="layui-collapse" lay-filter="component-panel">
						<div class="layui-colla-item">
							<h2 class="layui-colla-title">搜索条件</h2>
							<div class="layui-colla-content">
								<div class="accessToken-table-reload-btn layui-form" style="margin-bottom: 10px;">
									编码：
									<div class="layui-inline">
										<input class="layui-input" name="code" autocomplete="off">
									</div>
									企业ID：
									<div class="layui-inline">
										<input class="layui-input" name="corpId" autocomplete="off">
									</div>
									凭证密钥：
									<div class="layui-inline">
										<input class="layui-input" name="corpSecret" autocomplete="off">
									</div>
									代理Id：
									<div class="layui-inline">
										<select name="agentId"  id="agentId"  class="layui-select" >
										</select>
									</div>
									appId：
									<div class="layui-inline">
										<input class="layui-input" name="appId" autocomplete="off">
									</div>
									token：
									<div class="layui-inline">
										<input class="layui-input" name="token" autocomplete="off">
									</div>
									访问token：
									<div class="layui-inline">
										<input class="layui-input" name="accessToken" autocomplete="off">
									</div>
									aesKey：
									<div class="layui-inline">
										<input class="layui-input" name="aesKey" autocomplete="off">
									</div>
									oauth2redirectUri：
									<div class="layui-inline">
										<input class="layui-input" name="oauth2redirectUri" autocomplete="off">
									</div>
									jsapiTicketExpiresTime：
									<div class="layui-inline">
										<input class="layui-input" name="jsapiTicketExpiresTime" autocomplete="off">
									</div>
									过期时间：
									<div class="layui-inline">
										<input class="layui-input" name="expiresIn" autocomplete="off">
									</div>
									生成时间：
									<div class="layui-inline">
										<input class="layui-input" name="time" autocomplete="off">
									</div>
									创建时间：
									<div class="layui-inline">
										<input type="text" class="layui-input" id="createTime-laydate-range" placeholder=" - ">
										<input type="hidden" name="createTimeStart">
										<input type="hidden" name="createTimeEnd">
									</div>
									修改时间：
									<div class="layui-inline">
										<input type="text" class="layui-input" id="modifyTime-laydate-range" placeholder=" - ">
										<input type="hidden" name="modifyTimeStart">
										<input type="hidden" name="modifyTimeEnd">
									</div>
									<button class="layui-btn" data-type="reload">查询</button>
								</div>
							</div>
						</div>
					</div>
					<table class="layui-hide" id="accessTokenListTable" lay-filter="accessTokenListTable"></table>
					<script type="text/html" id="accessTokenListTable-toolbar">
						<div class="layui-btn-container">
							<button class="layui-btn layui-btn-sm" lay-event="addRec">添加</button>
							<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delRec">删除</button>
						</div>
					</script>

					<script type="text/html" id="accessTokenListTable-bar">
						<a class="layui-btn layui-btn-xs" lay-event="editRec">编辑</a>
					</script>

				</div>
			</div>
		</div>
	</div>
</div>
<script src="${ctxLayui}/layuiadmin/layui/layui.js"></script>
<script>
	layui.config({
		base: '${ctxLayui}/layuiadmin/'
	}).extend({
		index: 'lib/index'
	}).use(['index', 'table','dict','laydate','util'], function(){
		var $ = layui.$,table = layui.table,form = layui.form;
		var dict = layui.dict;
		var laydate = layui.laydate;
		var admin = layui.admin;
		var util = layui.util;
		dict.setSelect("agentId","agentId");
		laydate.render({
			elem: '#createTime-laydate-range' ,
			range: true,
			done: function(value, date, endDate){
				if(!value){
					$(".accessToken-table-reload-btn [name=createTimeStart]").val(null);
					$(".accessToken-table-reload-btn [name=createTimeEnd]").val(null);
					return;
				}
				var dateStr1= value.split(" - ")[0]
				var dateStr2= value.split(" - ")[1]
				$(".accessToken-table-reload-btn [name=createTimeStart]").val(dateStr1);
				$(".accessToken-table-reload-btn [name=createTimeEnd]").val(dateStr2);
			}
		});
		laydate.render({
			elem: '#modifyTime-laydate-range' ,
			range: true,
			done: function(value, date, endDate){
				if(!value){
					$(".accessToken-table-reload-btn [name=modifyTimeStart]").val(null);
					$(".accessToken-table-reload-btn [name=modifyTimeEnd]").val(null);
					return;
				}
				var dateStr1= value.split(" - ")[0]
				var dateStr2= value.split(" - ")[1]
				$(".accessToken-table-reload-btn [name=modifyTimeStart]").val(dateStr1);
				$(".accessToken-table-reload-btn [name=modifyTimeEnd]").val(dateStr2);
			}
		});
		var tplAgentId = function(d){
			var html = dict.showName("agentId",d.agentId);
			return html;
		};
		var tplCreateTime = function(d){
			if(d.modifyTime){
				return util.toDateString(d.modifyTime);
			}
			return '';
		};
		var tplModifyTime = function(d){
			if(d.modifyTime){
				return util.toDateString(d.modifyTime);
			}
			return '';
		};

		var preWhere = {};
		var tableIns = table.render({
			elem:'#accessTokenListTable'
			,url: ctx+'/system/accessToken/query'
			,cellMinWidth: 80
			,toolbar: '#accessTokenListTable-toolbar'
			,cols: [[
				{type: 'checkbox', fixed: 'left'}
				,{field:'code', title: '编码', align: 'center',minWidth:100 }
				,{field:'corpId', title: '企业ID', align: 'center',minWidth:100 }
				,{field:'corpSecret', title: '凭证密钥', align: 'center',minWidth:100 }
				,{field:'agentId', title: '代理Id', align: 'center',minWidth:100,templet:tplAgentId}
				,{field:'appId', title: 'appId', align: 'center',minWidth:100 }
				,{field:'token', title: 'token', align: 'center',minWidth:100 }
				,{field:'accessToken', title: '访问token', align: 'center',minWidth:100 }
				,{field:'aesKey', title: 'aesKey', align: 'center',minWidth:100 }
				,{field:'oauth2redirectUri', title: 'oauth2redirectUri', align: 'center',minWidth:100 }
				,{field:'jsapiTicketExpiresTime', title: 'jsapiTicketExpiresTime', align: 'center',minWidth:100 }
				,{field:'expiresIn', title: '过期时间', align: 'center',minWidth:100 }
				,{field:'time', title: '生成时间', align: 'center',minWidth:100 }
				,{field:'createTime', title: '创建时间', align: 'center',minWidth:100,templet:tplCreateTime}
				,{field:'modifyTime', title: '修改时间', align: 'center',minWidth:100,templet:tplModifyTime}
				,{title:'操作', toolbar: '#accessTokenListTable-bar', width:80}
			]]
			,page: true,
			done:function(){
				//this.where={}
				this.where = preWhere;
			}
		});
		var active = {
			addRec: function(){
				window.formData = null;
				layer.open({
					type: 2
					,title: '用户信息'
					,content: 'accessTokenForm.jsp'
					,maxmin: true
					,area: layui.setter.area
					,btn: ['确定', '取消']
					,yes: function(index, layero){
						var iframeWindow = window['layui-layer-iframe'+ index]
							,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
						iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
							var field = data.field;
							var rtn = admin.syncReq(ctx+"/system/accessToken/add",field);
							if(rtn && rtn.code==0){
								layer.msg('操作成功');
								active.reload();
								form.render();
								layer.close(index);
							}else{
								layer.msg('操作失败');
							}
						});
						submit.trigger('click');
					}
				});
			},
			editRec:function(obj){
				var rec = obj.data;
				window.formData = null;
				layer.open({
					type: 2
					,title: '用户信息'
					,content: 'accessTokenForm.jsp'
					,maxmin: true
					,area: layui.setter.area
					,btn: ['确定', '取消']
					,success:function(layero,index){
						var body = layer.getChildFrame('body', index);
						var data = obj.data;
						window.formData = data;
						$.each(data,function(item){
							body.find("[name="+item+"]").val(data[item]);
						});
					}
					,yes: function(index, layero){
						var iframeWindow = window['layui-layer-iframe'+ index]
							,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
						iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
							var field = data.field; 
							field.id = rec.id;
							var rtn = admin.syncReq(ctx+"/system/accessToken/modify",field);
							if(rtn && rtn.code==0){
								layer.msg('操作成功');
								active.reload();
								form.render();
								layer.close(index);
							}else{
								layer.msg('操作失败');
							}
						});
						submit.trigger('click');
					}
				});
			},
			delRec:function () {
				var checkStatus = table.checkStatus('accessTokenListTable')
				var recs = checkStatus.data;
				if(!recs || recs.length<=0){
					layer.msg('请至少选择一条记录');
					return;
				}
				layer.confirm('确定删除'+recs.length+'条记录?', function(index){
					var idArr = [];
					for(var i=0;i<recs.length;i++){
						var rec = recs[i];
						idArr.push(rec.id);
					}
					var rtn = admin.syncReq(ctx+"/system/accessToken/deleteByBatch",{"ids":idArr.join(",")});
					if(rtn && rtn.code==0){
						layer.msg('操作成功');
						active.reload();
					}else{
						layer.msg('操作失败');
					}
				});
			},
			reload: function(){
				var params = active.getWhere();
				preWhere = params;
				params.page = 1
				var config = {};
				config.where = params;
				tableIns.reload(config);
			},
			getWhere:function () {
				var params = {};
				var els = $(".accessToken-table-reload-btn [name]");
				for(var i=0;i<els.length;i++){
					var _e = $(els[i]);
					var eVal = _e.val();
					var eName = _e.attr("name");
					if(eVal){
						params[eName] = eVal;
					}
				}
				return params;
			}
		};
		table.on('toolbar(accessTokenListTable)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id);
			switch(obj.event){
				case 'addRec':
					active.addRec()
					break;
				case 'delRec':
					active.delRec(checkStatus)
					break;
			};
		});
		table.on('tool(accessTokenListTable)', function(obj){
			var data = obj.data;
			if(obj.event === 'editRec'){
				active.editRec(obj);
			}
		});
		$('.accessToken-table-reload-btn .layui-btn').on('click', function(){
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
	});
</script>
</body>
</html>
